package schooltg.core.persist.service.impl;

import schooltg.core.data.Education;
import schooltg.core.data.User;
import schooltg.core.persist.dao.EducationDao;
import schooltg.core.persist.entity.EducationPO;
import schooltg.core.persist.service.EducationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import schooltg.core.persist.utils.BeanMapUtils;

import java.util.*;

/**
 * Created by liuyangfx on 16-4-25.
 */
@Service
public class EducationServiceImpl implements EducationService{
    @Autowired
    private EducationDao educationDao;
    @Override
    public Education getEducationByUserId(long id) {
       EducationPO educationPO =  educationDao.getEducationByUserId(id);
        Education education = new Education();
        if(educationPO != null) {
            BeanUtils.copyProperties(educationPO, education);
            return education;
        }
        return null;
    }

    public Map<Long, Education> getEducationByUserIds(Set<Long> ids){
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyMap();
        }
        List<EducationPO> list = educationDao.getEducationByUserIds(ids);
        Map<Long, Education> ret = new HashMap<>();
        list.forEach(po -> {
            ret.put(po.getId(), BeanMapUtils.copy(po));
        });
        return ret;
    }



    @Override
    public Education insertEducation(Education education) {
        educationDao.insertEducation(education);
        return null;
    }

    @Override
    public void saveOrUpdateEducation(Education education) {
        EducationPO educationPO = educationDao.getEducationByUserId(education.getUserId());
        if(educationPO != null){
            educationDao.updateEducation(education);
        }else{
            educationDao.insertEducation(education);
        }
    }



}
